package com.tuniu.agents.impl.train.logger;

import com.tuniu.agents.impl.train.enums.TrainToolEnum;
import com.tuniu.agents.message.ToolLogger;
import com.tuniu.agents.tools.train.constants.TrainCommonConstants;
import com.tuniu.agents.tools.train.tool.SearchAllTrainTool;
import com.tuniu.agents.tools.train.tool.SearchConditionTrainTool;
import com.tuniu.agents.util.I18nUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service(TrainToolLoggingService.BEAN_NAME)
@Slf4j
public class TrainToolLoggingService implements ToolLogger {

    public static final String BEAN_NAME = "trainToolLoggingService";

    @Override
    @SneakyThrows
    public List<String> before(Object request, ToolContext context) {
        String conversationId = (String)context.getContext().get(TrainCommonConstants.CONVERSATION_ID);
        String[] loadingStr = TrainToolEnum.ALL_TRAIN.getBefore().split(",");

        if(request instanceof SearchAllTrainTool.Request trainRequest) {
            loadingStr[0] = I18nUtil.getMessage(conversationId,loadingStr[0], new Object[]{trainRequest.departureDate(), trainRequest.departureCityName(), trainRequest.arrivalCityName()});
        }else if(request instanceof SearchConditionTrainTool.Request trainRequest){
            loadingStr[0] = I18nUtil.getMessage(conversationId,loadingStr[0],new Object[]{trainRequest.departureDate(),trainRequest.departureCityName() ,trainRequest.arrivalCityName()});
        }else {
            return Collections.emptyList();
        }
        loadingStr[1] = I18nUtil.getMessage(conversationId,loadingStr[1]);
        return Arrays.stream(loadingStr).toList();
    }

    @Override
    @SneakyThrows
    public List<String> after(Object request, ToolContext context, Object response) {
        SearchAllTrainTool.Response result = (SearchAllTrainTool.Response) response;
        String conversationId = (String)context.getContext().get(TrainCommonConstants.CONVERSATION_ID);
        if(response != null) {
            return List.of(I18nUtil.getMessage(conversationId,TrainToolEnum.ALL_TRAIN.getAfter(),new Object[]{result.data().size()}));
        }
        return Collections.emptyList();
    }
}
